package com.qk.controller;


import com.qk.common.Result;
import com.qk.utils.AliyunOSSOperator;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.util.UUID;

@Slf4j
@RestController
public class UploadController {
    /*@PostMapping("/upload")
    public Result upload(String name, Integer age, MultipartFile file) throws Exception {
        log.info("name: {}, age: {}, file: {}", name, age, file.getOriginalFilename());
        //1、构造文件名（不可重复） ->使用uuid
        // 获取原文件名
        String fileName = file.getOriginalFilename();
        // 截取原文件名后缀
        String extName = fileName.substring(fileName.lastIndexOf("."));
        // 使用uuid 构建新的文件名
        String newFileName = UUID.randomUUID().toString() + extName;

        // 指定保存路径
        file.transferTo(new File("D:\\images\\"+newFileName));
        return Result.success();
    }*/
    @Autowired
    private AliyunOSSOperator aliyunOSSOperator;
    @PostMapping("/upload")
    public Result upload(MultipartFile image) throws Exception {
       log.info("文件上传{}", image);
        //1、构造文件名（不可重复） ->使用uuid
        // 获取原文件名
        String fileName = image.getOriginalFilename();
        // 截取原文件名后缀
        String extName = fileName.substring(fileName.lastIndexOf("."));
        // 使用uuid 构建新的文件名
        String newFileName = UUID.randomUUID().toString() + extName;
        String url = aliyunOSSOperator.upload(image.getBytes(), newFileName);
        return Result.success(url);
    }

}
